#
# LuckyBot4, a python IRC bot
# (c) Copyright 2008 by Lucas van Dijk
# http://www.return1.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
#
# $Id: deck.py 29 2009-02-11 15:44:45Z luckyluke56 $
#

from luckybot.luckynet.protocols.irc import Format
import random


class Card(object):
	"""
		Represents a card in a deck
	"""

	SUIT_HEARTS = 1
	SUIT_SPADES = 2
	SUIT_CLUBS = 3
	SUIT_DIAMONDS = 4

	TYPE_2 = 2
	TYPE_3 = 3
	TYPE_4 = 4
	TYPE_5 = 5
	TYPE_6 = 6
	TYPE_7 = 7
	TYPE_8 = 8
	TYPE_9 = 9
	TYPE_10 = 10
	TYPE_JACK = 11
	TYPE_QUEEN = 12
	TYPE_KING = 13
	TYPE_ACE = 14

	def __init__(self, suit, type):
		self.suit = suit
		self.type = type

	def suited(self, other):
		return self.suit == other.suit

	def __eq__(self, other):
		return other.suit == self.suit and other.type == self.type

	def __ne__(self, other):
		return other.suit != self.suit or other.type != self.type

	def __lt__(self, other):
		return self.type < other.type

	def __le__(self, other):
		return self.type <= other.type

	def __gt__(self, other):
		return self.type > other.type

	def __ge__(self, other):
		return self.type >= other.type

	def __unicode__(self):
		if self.suit == self.SUIT_HEARTS:
			return u'%s\u2665%s' % (Format.color('red'), Format.normal())
		elif self.suit == self.SUIT_DIAMONDS:
			return u'%s\u2666%s' % (Format.color('red'), Format.normal())
		elif self.suit == self.SUIT_CLUBS:
			return u'\u2663'
		elif self.suit == self.SUIT_SPADES:
			return u'\u2660'

class Collection(object):
	"""
		Represents a collection of cards
	"""

	def __init__(self):
		self.cards = []

	def add_card(self, card):
		if isinstance(card, Card):
			self.cards.append(card)
		else:
			raise ValueError, "Invalid object given for 'card'"

class Deck(Collection):
	"""
		Represents a deck
	"""

	def fill(self):
		self.cards = []

		i = 1
		while i <= 4:
			j = 1
			while j <= 14:
				card = Card(i, j)
				self.add_card(card)

	def shuffle(self):
		random.shuffle(self.cards)



